var http = require('http');
var querystring = require('querystring');




//
// http://www.imooc.com/course/docomment
// content:正在看还是不错的！
// cid:706
//
// ****** headers ******
//Accept:application/json, text/javascript, */*; q=0.01
// Accept-Encoding:gzip, deflate
// Accept-Language:zh-CN,zh;q=0.8
// Connection:keep-alive
// Content-Length:97
// Content-Type:application/x-www-form-urlencoded; charset=UTF-8
// Cookie:imooc_uuid=5d63efca-9dc8-42f4-8281-cf62c6fab4f7; imooc_isnew_ct=1479215568; loginstate=1; apsid=JkOTY0NWMxNGYzMDRjOWRkNTljMTgxM2Q2NjVjYWUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjgzNTc5OQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABpc21yd2FuZ0BnbWFpbC5jb20AAAAAAAAAAAAAAAAAADRhODlmN2JhOWUzNmNhODViOGQ0NzQ0N2Y0ZDFmOTc2HAstWBwLLVg%3DOD; last_login_username=ismrwang%40gmail.com; PHPSESSID=hk2c7uf3lifbqoe5b4nqoes430; jwplayer.qualityLabel=é«æ¸; jwplayer.volume=100; imooc_isnew=2; cvde=58395ed76ccb4-156; IMCDNS=0; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1479711161,1480080954,1480154839,1480394493; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1480478945
// Host:www.imooc.com
// Origin:http://www.imooc.com
// Referer:http://www.imooc.com/comment/706
// User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36
// X-Requested-With:XMLHttpRequest
//




var postData = querystring.stringify({
    'content': '慢慢尝试一下吧',
    'cid': 348
});

var options = {
    'hostname': 'www.imooc.com',
    'port': 80,
    'path':'/course/docomment',
    'method': 'POST',
    'headers': {
        'Accept':'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding':'gzip, deflate',
        'Accept-Language':'zh-CN,zh;q=0.8',
        'Connection':'keep-alive',
        'Content-Length': postData.length,
        'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie':'imooc_uuid=5d63efca-9dc8-42f4-8281-cf62c6fab4f7; imooc_isnew_ct=1479215568; loginstate=1; apsid=JkOTY0NWMxNGYzMDRjOWRkNTljMTgxM2Q2NjVjYWUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjgzNTc5OQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABpc21yd2FuZ0BnbWFpbC5jb20AAAAAAAAAAAAAAAAAADRhODlmN2JhOWUzNmNhODViOGQ0NzQ0N2Y0ZDFmOTc2HAstWBwLLVg%3DOD; last_login_username=ismrwang%40gmail.com; PHPSESSID=hk2c7uf3lifbqoe5b4nqoes430; jwplayer.qualityLabel=é«æ¸; jwplayer.volume=100; imooc_isnew=2; cvde=58395ed76ccb4-175; IMCDNS=0; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1479711161,1480080954,1480154839,1480394493; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1480483008',
        'Host':'www.imooc.com',
        'Origin':'http://www.imooc.com',
        'Referer':'http://www.imooc.com/course/comment/id/348?page=3',
        'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
        'X-Requested-With':'XMLHttpRequest'
    }
}

var req = http.request(options, (res) => {
    console.log(`STATUS: ${res.statusCode}`);
    console.log(`STATUS: ${JSON.stringify(res.headers)}`);

    res.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
    });
    res.on('end', () => {
        console.log('FINISHED');
    })
})

req.on('error', function (e) {
    console.log(`ERROR: ${e.message}`);
})
console.log(postData.length);
// write data to request body
req.write(postData);
req.end();
